package Framework;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import connection.Connection;

public class GameSelectFrame extends JFrame implements ActionListener {

	Connection conn;
	Main main;

	private JPanel gameSelectPanel;
	private JPanel sideSelect = new JPanel();

	private JLabel selectGameLabel = new JLabel("Selecteer een spel:");
	private JLabel selectPlayerLabel = new JLabel("Selecteer een tegenstander:");
	private JLabel gameModeLabel = new JLabel("Selecteer hoe u wilt spelen:");

	private ButtonGroup sideGroup = new ButtonGroup();

	private JRadioButton computer = new JRadioButton("Computer");
	private JRadioButton human = new JRadioButton("Mens");

	private JButton chalButton = new JButton("Challenge");
	private JButton subButton = new JButton("Subscribe");
	private JButton refresh = new JButton("Refresh");

	private JComboBox playerBox;
	private JComboBox gameBox;

	private String player;

	public GameSelectFrame(Main main, String player) {
		this.main = main;

		chalButton.addActionListener(this);
		subButton.addActionListener(this);
		refresh.addActionListener(this);

		gameSelectPanel = new JPanel();
		gameSelectPanel.setLayout(new GridLayout(0, 2));

		gameSelectPanel.add(selectGameLabel);
		main.lock.lock();
		try {
			gameBox = new JComboBox(main.c.gameList);
			gameBox.addActionListener(this);
			gameBox.setSelectedItem(main.c.gameList[1]);
			gameSelectPanel.add(gameBox);
			playerBox = new JComboBox();
			updatePlayerList(main.c.playerList);
			gameSelectPanel.add(selectPlayerLabel);
			gameSelectPanel.add(playerBox);
			gameSelectPanel.add(refresh);
			gameSelectPanel.add(new JLabel(""));
		} finally {
			main.lock.unlock();
		}

		gameSelectPanel.add(gameModeLabel);
		human.setSelected(true);
		sideSelect.add(human);
		sideSelect.add(computer);
		sideGroup.add(human);
		sideGroup.add(computer);
		human.addActionListener(this);
		computer.addActionListener(this);
		gameSelectPanel.add(sideSelect);

		gameSelectPanel.add(chalButton);
		gameSelectPanel.add(subButton);

		this.player = player;
		this.setTitle(player);

		add(gameSelectPanel);

		setSize(400, 400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == subButton) {
			main.c.subscribe((String) gameBox.getSelectedItem());
			try {
				Thread.sleep(500);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			if (main.gui == null) {
				JOptionPane
						.showMessageDialog(
								this,
								"U heeft zich ingescheven voor "
										+ (String) gameBox.getSelectedItem()
										+ ". Wacht alstublieft tot een tegenstander beschikbaar is.");
			}
		}
		if (e.getSource() == chalButton) {
			String player = (String) playerBox.getSelectedItem();
			if (player.equals("") || player.equals("" + player)) {
				JOptionPane.showMessageDialog(this,
						"U moet een tegenstander selecteren.");
			} else {
				main.c.challenge(player, (String) gameBox.getSelectedItem());
			}
		}
		if (e.getSource() == refresh) {
			main.c.getPlayerList();
			updatePlayerList(main.c.playerList);
		}
		if (e.getSource() == computer) {
			main.mode = 1;
		} else if (e.getSource() == human) {
			main.mode = 0;
		}
	}

	private void updatePlayerList(String[] players) {
		main.lock.lock();
		try {
			if (players != null) {
				playerBox.removeAllItems();
				int x = 0;
				for (int i = 0; i < players.length; i++) {
					if (!players[i].equals(player)) {
						playerBox.insertItemAt(players[i], x);
						if (x == 0) {
							playerBox.setSelectedIndex(x);
						}
						x++;
					}
				}
			}
		} finally {
			main.lock.unlock();
		}
	}
}